package com.irdstudio.tdp.executor.core.plugin.gencode;

import com.irdstudio.tdp.executor.core.plugin.AbstractPlugin;
import com.irdstudio.tdp.executor.core.plugin.util.MyFileUtil;
import com.irdstudio.tdp.executor.core.plugin.util.ProjectGenerateUtil;
import com.irdstudio.tdp.executor.rest.init.ExecutorInstInfo;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * 初始化应用原型工程
 * @author ligm
 * @date 2019-10-14
 */
public class AppProjectCopyPlugin extends AbstractPlugin {

	String subsCode = null;
	/** 读取插件所需要参数 */
	@Override
	protected boolean doReadConfigureFromDB(Connection conn, String szConfIdentify) throws SQLException {
		subsCode = context.getVv().getValue("subs_code");

		return true;
	}

	@Override
	public boolean execute() {
		
		logger.info("调用初始化应用原型工程插件,复制并替换应用模板，生成{}工程" + subsCode);
		
		// 如果应用工程目录已经存在，则不执行原制工程步骤
		String outFilePath = ExecutorInstInfo.PROJECT_OUTPUT_PATH + subsCode;
		if(MyFileUtil.checkFileExist(outFilePath)) {
			//return true;
			try {
				FileUtils.forceDelete(new File(outFilePath));
			} catch (IOException e) {
				// 删除原文件异常
				e.printStackTrace();
				return false;
			}
		}


		// 获取应用模板实际路径与复制后的存放路径
		String srcStr = "single.template";
		String srcFilePath = ExecutorInstInfo.PROJECT_PATH + File.separator + srcStr;
		String replaceStr = subsCode;
        
        // 复制并重命名工程
        ProjectGenerateUtil.copyAndRenameProject(srcFilePath, outFilePath, srcStr, replaceStr);
        
		return true;
	}

}
